/*
 * Enemy.cpp
 *
 *  Created on: Jan 4, 2009
 *      Author: Carmine Red
 */

#include "Enemy.h"

Enemy::Enemy()
{
	health = 1;
	speed = 1;

	//load texture
	HR(D3DXCreateTextureFromFile(gd3dDevice, "Textures//EnemyTest.bmp", &m_ESprite));
	//m_EPos = D3DXVECTOR3(207.5f, 102.5f, 0.0f);
	m_EPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
}

Enemy::~Enemy()
{
	//ReleaseCOM(m_ESprite);
}

void Enemy::cleanUp()
{
	ReleaseCOM(m_ESprite);
}

void Enemy::moveEnemy()	// this moves the enemy
{
	switch(dir)
	{
	case UP:
		m_EPos.y += speed;
		break;
	case DOWN:
		m_EPos.y -= speed;
		break;
	case RIGHT:
		m_EPos.x += speed;
		break;
	case LEFT:
		m_EPos.x -= speed;
		break;
	}
}

void Enemy::checkTile(float xUse, float yUse, int tileDir)
							// this updates the enemy based on the tile data passed
{
	// it also uses the DIRTILESIDE that's been #defined
	// it compares the enemy position to the xUse and yUse coordinates of the tile
	// if the enemy is "beyond" the tile coordinates as per the direction, we'll
	// reset the enemy coordinates to the tile corrdinates, adjust it's offset based
	// on their current discrepancy (so no movement is waster) and set the enemy's new
	// direction.

	if(tileDir == dir)
	{
		// if the tileDir and dir are already the same, we don't need to do anything
		return;
	}

	// otherwise we'll see if it's gone beyond the activation point

	bool redirect = false;
	float offset;

	switch(dir)
	{
	case UP:
		if(m_EPos.y >= yUse)
		{
			redirect = true;
			offset = m_EPos.y - yUse;
		}
		break;
	case DOWN:
		if(m_EPos.y < yUse)
		{
			redirect = true;
			offset = yUse - m_EPos.y;
		}
		break;
	case RIGHT:
		if(m_EPos.x >= xUse)
		{
			redirect = true;
			offset = m_EPos.x - xUse;
		}
		break;
	case LEFT:
		if(m_EPos.x < xUse)
		{
			redirect = true;
			offset = xUse - m_EPos.x;
		}
		break;
	default:
		break;
	}

	// if it has passed the activation point, then we set its
	//	new position, new direction, and apply the offeset in
	//	that new direction

	// PONDERING: if we make directional tiles have effects, might
	//	we enact them here?

	if(redirect == true)
	{
		m_EPos.x = xUse;
		m_EPos.y = yUse;
		dir = tileDir;

		switch(dir)
		{
		case UP:
			m_EPos.y += offset;
			break;
		case DOWN:
			m_EPos.y -= offset;
			break;
		case RIGHT:
			m_EPos.x += offset;
			break;
		case LEFT:
			m_EPos.x -= offset;
			break;
		}
	}
}

void Enemy::render(Prismatic* game)
{
	// drawwwwwwwwwwwwwwwww

	D3DXMATRIX matTrans;

	D3DXMatrixTranslation(&matTrans, m_EPos.x, m_EPos.y, m_EPos.z);

	//set flags for this render and begin
	HR(game->Sprite()->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));

	HR(game->Sprite()->SetTransform(&matTrans));

	//for some reason our x and y values are flipped
	// WHHHHHHHHHHHYYYYYYYYYYYYYYYYYYYYY

	//D3DXVECTOR3 debugPosData(-m_EPos.x, -m_EPos.y, m_EPos.z);
	//D3DXVECTOR3 debugPosData(0.0, 0.0, 0.0);

	// account for center of sprite
	float ENEMYSIDE = 32;
	D3DXVECTOR3 center(ENEMYSIDE * 0.5, ENEMYSIDE * 0.5, 0.0);

	//debugPosData.x += ENEMYSIDE * 0.5;
	//debugPosData.y += ENEMYSIDE * 0.5;

	//draw background, plain and simple, not doing any translations or anything
	//but if i was, they would go here as well
	HR(game->Sprite()->Draw(m_ESprite, 0, &center, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(game->Sprite()->Flush());

	//be sure to end when done
	HR(game->Sprite()->End());
}
